/*  This file is part of the comicconquer project
    Copyright (C) 2010 Reach <reach334@gmail.com>

    ComicConquer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This software is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public 
    License along with this software.  If not, see <http://www.gnu.org/licenses/>.

*/

#ifndef DISPLAYCONTROLER_H
#define DISPLAYCONTROLER_H

#include "PageStruct.h"

/**
 * the rects to control display
 * 
 * @author reach (5/25/2010)
 */
struct DisplayRect
{
    QRect rcPic; // rect of image
    QRect rcDisplay; // the display part of image
    QRect rcScreen; // display rect in screen
};
typedef QVector<DisplayRect> DisplayRectVec;

/**
 * Datas needed for display a page.
 * 
 * @author reach (5/25/2010)
 */
struct DisplayPage
{
    DivPage* page;
    DisplayRectVec displayRects;

    DisplayPage()
    {
        displayRects.resize(Cot_End);
    }
};
typedef QList<DisplayPage> DisplayPageList;

/**
 * Control page display on screen.
 * Include the operations drag, move, scroll page and change content.
 * 
 * @author reach (5/25/2010)
 */
class DisplayControler : public QObject
{
    Q_OBJECT

    //-- constructor
public:
    DisplayControler(CoconProfile* profile, QObject* parent = 0);

    //-- public methods
public:
    void addPage(DivPage* page, bool isPrev = false);
    DivPage* getPage(); // get the firest page
    void paint(QPainter& painter, QVector<QRgb>& colorTable);
    void drag(const QPoint& offset);

    void scrollUp();
    void scrollDown();
    void scrollLeft();
    void scrollRight();

    void moveUp();
    void moveDown();
    void moveLeft();
    void moveRight();

    void toggleOrigin(const QPoint& pos = QPoint(0, 0));

    void setContent(int displayContent);
    int getContent();
    void restoreContent();
    void recoverContent();

    bool isEmpty();
    void clearPages();
    void removeFirstPage();
    void removeLastPage();

    //-- signals
    signals:
    void prevPage();
    void nextPage();
    void prevComic();
    void nextComic();
    void requestPage(int iPage, int iDivPage = 0, bool isPrev = false);

    //-- private methods
private:
    void correctRect(QRect& rcDisplay, QRect& rcPic);

    //-- private members
private:
    DisplayPageList m_pageList;
    CoconProfile* m_profile;

    int m_content;
    int m_restoredContent;
    int m_gridId;

    bool m_isPrev;
    bool m_isMoveUp;
};

#endif // DISPLAYCONTROLER_H
